package com.sdry.service.impl.hpf;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.hpf.BomMapper;
import com.sdry.mapper.hpf.PartMapper;
import com.sdry.model.hpf.Bom;
import com.sdry.model.hpf.Part;
import com.sdry.service.hpf.PartService;

/**
 * @ClassName: PartServiceImpl 
 * @Description:零件信息Service实现
 * @Author hpf
 * @Date 2018年9月20日 下午7:07:55 
 * @version 1.0
 */
@Service("partService")
public class PartServiceImpl implements PartService {

	@Resource
	private PartMapper partMapper;
	@Resource
	private BomMapper bomMapper;
	
	/**
	 * 查询零件信息总数
	 * @param Part
	 * @return
	 */
	public Integer countPart(Part part) {
		// TODO Auto-generated method stub
		return partMapper.countPart(part);
	}

	/**
	 * 分页查询零件信息
	 * @param map
	 * @return
	 */
	public List<Part> listPagePart(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return partMapper.listPagePart(map);
	}

	/**
	 * 新增零件信息
	 * @param Part
	 * @return
	 */
	public Integer insertPart(Part part) {
		int isSuccess = -2;
		Part pa = partMapper.findPartByPanum(part.getPanum());
		if(null == pa){
			int i = partMapper.insertPart(part);
			if(i > 0){
				isSuccess = 2;
			}
		}
		return isSuccess;
	}

	/**
	 * 修改零件信息
	 * @param Part
	 * @return
	 */
	public Integer updatePart(Part part) {
		int isSuccess = -3;
		Part p = partMapper.findPartByPaid(part.getPaid());
		if(p.getPanum().equals(part.getPanum())){
			int i = partMapper.updatePart(part);
			if(i > 0){
				isSuccess =  3;
			}
		}else{
			Part pa = partMapper.findPartByPanum(part.getPanum());
			if(null == pa){
				int i = partMapper.updatePart(part);
				if(i > 0){
					isSuccess = 3;
				}
			}
		}
		return isSuccess;
	}

	/**
	 * 校验是否删除配件信息
	 * @param str 配件信息ID组成的字符串
	 * @return
	 */
	@Override
	public Integer deletePartOfVerify(String str) {
		int isSuccess = -1;
		String[] split = str.split(",");
		for(int i = 0; i < split.length; i ++){
			Long id = Long.parseLong(split[i]);
			List<Bom> bomList = bomMapper.getBomByPaid(id);
			if(bomList.size() > 0){
				isSuccess = 1;
				break;
			}
		}
		return isSuccess;
	}
	
	/**
	 * 删除零件信息
	 * @param str 配件信息ID组成的字符串
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public Integer deletePart(String str) {
		// TODO Auto-generated method stub
		int isSuccess = 0;
		try {
			String[] idArr = str.split(",");
			for (int i = 0; i < idArr.length; i++) {
				isSuccess = partMapper.deletePart(Long.parseLong(idArr[i]));
			}
		} catch (Exception e) {
			e.printStackTrace();     
	        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	        isSuccess = 0;
		}
		return isSuccess;
	}

	/**
	 * 查询所有零件信息
	 * @return
	 */
	@Override
	public List<Part> listAllPart() {
		// TODO Auto-generated method stub
		return partMapper.listAllPart();
	}

	/**
	 * 根据编号查询零件信息
	 * @param panum
	 * @return
	 */
	public Part findPartByPanum(String panum) {
		return partMapper.findPartByPanum(panum);
	}

	/**       
	 * @param paid
	 * @return    
	 */
	@Override
	public Part getPartByPaid(Long paid) {
		return partMapper.findPartByPaid(paid);
	}

}
